Sprint Boot是一個輕量級的Java應用程序框架,而我們會使用kotlin來開發~
Sprint Boot在2022年11月24日升級到3.0.0,是近期很大的改動,
Spring Boot 3.0 最低要求 Java 17,向上到 Java 20。
我們要確認SDK是符合的。
另外Sprint 框架 要是Spring Framework 6.0.11或以上。
安裝完Java、Maven後,我們可以開始來玩sprint boot了!
我使用Maven來建構project
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>kotlin-project</artifactId>
    <groupId>org.example</groupId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>consoleApp</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.3</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <kotlin.code.style>official</kotlin.code.style>
        <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
    </properties>
    <repositories>
        <repository>
            <id>mavenCentral</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>
    <build>
        <sourceDirectory>src/main/kotlin</sourceDirectory>
        <testSourceDirectory>src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>1.9.0</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>MainKt</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit5</artifactId>
            <version>1.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>1.9.0</version>
        </dependency>
    </dependencies>
</project>
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@SpringBootApplication
open class MyApplication {
    @RequestMapping("/")
    fun firstApi() = "IT Home Iron man!!"
}
fun main(args: Array<String>) {
    runApplication<MyApplication>(*args)
}
這是我們第一個web app! 可以在瀏覽器中打localhost:8080 即可看到我們的 IT Home Iron man!!
如同以往的Sprint Boot,這邊來解釋一下出現的annotation
它就是一個魔法,將web打過來的請求接收下來,Controller層就是接收外界資訊的地方,通常我們會將Controller層的資料打去Service層或是直接打到Repository層,取決於我們專案的大小以及設計。就是MVC(Model–view–controller)架構中的C。
它是負責我們的路由mapping,會告訴我們的程式具有此設定值的路徑(範例是使用"\")都要call firstApi 這個function。例如GET、POST、PUT都會打到firstApi 這個function。
這個annotation結合了@SpringBootConfiguration,@EnableAutoConfiguration和@ComponentScan
,這些我們之後再好好的研究一番!
這是遵循 Java app入口約定的標準方法。 它會delege SprintApplication的run,我們要將Myapplication當作參數告訴run這個method,讓SprintApplication知道哪一個是主要的spring component。
今天我們成功的把Kotlin spring boot 3啟動起來了! 希望我們不會卸載它(X

https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html